package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.admin.vo.CommonVo;
import com.cskaoyan.service.MarketKeywordService;
import com.cskaoyan.utils.CommonUtil;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (MarketKeyword)表控制层
 *
 * @author makejava
 * @since 2021-10-19 09:18:58
 */
@RestController
@RequestMapping("admin/keyword")
public class MarketKeywordController {
    /**
     * 服务对象
     */
    @Resource
    private MarketKeywordService marketKeywordService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequiresPermissions("admin:keyword:list")
    @GetMapping("list")
    public BaseRespVo list(String keyword, String url, BaseParam param) {
        String k = null;
        String u = null;
        if (keyword != null) k = "%" + keyword + "%";
        if (url != null) u = "%" + url + "%";
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<MarketKeyword> marketKeywords = marketKeywordService.queryByPage(k, u, param);
        int size = marketKeywords.size();
        CommonVo commonVo = new CommonVo();
        commonVo.setList(marketKeywords);
        commonVo.setTotal(size);
        commonVo.setPage(param.getPage());
        commonVo.setLimit(param.getLimit());
        commonVo.setPages(size / param.getLimit() + 1);
        return BaseRespVo.ok(commonVo);
    }


    /**
     * 新增数据
     *
     * @param marketKeyword 实体
     * @return 新增结果
     */
    @RequiresPermissions("admin:keyword:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody MarketKeyword marketKeyword) {
        if (CommonUtil.isNull(marketKeyword.getKeyword())) return BaseRespVo.fail("不可以为空");
        if (marketKeyword.getUrl() == null) marketKeyword.setUrl("");
        marketKeywordService.insert(marketKeyword);
        marketKeyword.setId(marketKeyword.getId());
        marketKeyword.setUpdateTime(CommonUtil.nowTime());
        marketKeyword.setAddTime(CommonUtil.nowTime());
        return BaseRespVo.ok(marketKeyword);
    }

    /**
     * 编辑数据
     *
     * @param marketKeyword 实体
     * @return 编辑结果
     */
    @RequiresPermissions("admin:keyword:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketKeyword marketKeyword) {
        if (CommonUtil.isNull(marketKeyword.getKeyword())) return BaseRespVo.fail("不可以为空");
        marketKeywordService.update(marketKeyword);
        marketKeyword.setUpdateTime(CommonUtil.nowTime());
        return BaseRespVo.ok(marketKeyword);
    }

    /**
     * 删除数据
     *
     * @param
     * @return 删除是否成功
     */
    @RequiresPermissions("admin:keyword:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketKeyword marketKeyword) {
        marketKeywordService.deleteById(marketKeyword.getId());
        return BaseRespVo.ok();
    }

}

